package com.lettcode;


/**
 * @Description
 * @Author Jlis
 * @Date 2021/7/21
 * @Version 1.0
 */
public class Lc0013 {

    public static void main(String[] args) {
        System.out.println(romanToInt("MCDLXXVI"));
    }
    public static int romanToInt(String s) {
        int ret = 0;
        for(int i = 0; i < s.length(); i++){
            Integer cur = getValue(s.charAt(i));
            if(i<s.length()-1 && cur<getValue(s.charAt(i+1))){
                ret = ret - cur;
           }else{
                ret = ret + cur;
            }

        }
        return ret;
    }

    private static int getValue(char ch) {
        switch(ch) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            default: return 0;
        }
    }
}
